package 斐波那契数列问题;

import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] cost = new int[n];
        for (int i = 0; i < n; i++) {
            cost[i] = in.nextInt();
        }
        System.out.println(minCostClimbingStairs(cost));
    }

    // 楼顶是整个数组外的位置,也就是nums[nums.length]
    // 将从n-2和从n-1楼上来的cost比较一下,最小值就是n的值

    public static int minCostClimbingStairs(int[] cost) {
        // 解法一: 以i位置为结尾
        int[] dp = new int[cost.length+1];
        dp[0] = dp[1] = 0; // 可以选择从哪一层走

        for (int i = 2; i <= cost.length; i++) {
            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[cost.length];
    }

    public static int minCostClimbingStairs2(int[] cost) {
        // 解法二: 以i位置为起点
        // dp[i]: 表示从i位置到楼顶的最小花费
        // 从右往左填表
        int n = cost.length;
        int[] dp = new int[n];
        dp[n-1] = cost[n-1];
        dp[n-2] = cost[n-2];
        for (int i = n-3; i >=0 ; i--) {
            dp[i] = Math.min(dp[i+1], dp[i+2])+cost[i];
        }
        return Math.min(dp[0],dp[1]);
    }
}
